package com.cennavi.function;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorLong;
import com.googlecode.aviator.runtime.type.AviatorObject;

import java.lang.reflect.Array;
import java.util.*;

/**
 * @author GongXings
 * @createTime 16 9:53
 * @description 获取集合中最小元素
 */
public class MinFunction extends AbstractFunction {
    @Override
    public String getName() {
        return "min";
    }
    @Override
    public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
        Object value = arg1.getValue(env);
        if (value == null) {
            throw new NullPointerException("null seq");
        }
        Class<?> clazz = value.getClass();
        Object min = null;
        if (Collection.class.isAssignableFrom(clazz)) {
            Collection<?> col = (Collection<?>) value;
            List list = new ArrayList<>(col);
            min = Collections.min(list);
        }else {
            throw new IllegalArgumentException(arg1.desc(env) + " 请传入list集合");
        }
        return AviatorLong.valueOf(min);
    }
}